package com.edu.schoolforum.utils;
import com.edu.schoolforum.enums.ResultEnum;
import lombok.Data;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description 统一的数据返回体
 * @Author tao
 * @Date 2021/8/4
 */
@Data
@ToString
public class CommonResult {
    private static Logger logger = LoggerFactory.getLogger(CommonResult.class);
    int resultCode;
    String msg;
    Object data;

    private CommonResult(int resultCode,
                         String msg,
                         Object data) {
        this.resultCode = resultCode;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 返回操作成功
     *
     * @return
     */
    public static CommonResult success() {
        return new CommonResult(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMsg(),
                null);
    }

    /**
     * 返回操作成功及数据
     *
     * @param data
     * @return
     */
    public static CommonResult success(Object data) {
        return new CommonResult(
                ResultEnum.SUCCESS.getCode(),
                ResultEnum.SUCCESS.getMsg(),
                data);
    }

    /**
     * 返回自定义异常状态码
     *
     * @param exceptionEnum
     * @return
     */
    public static CommonResult fail(ResultEnum exceptionEnum) {
        logger.info("执行失败："+exceptionEnum.getMsg());
        return new CommonResult(
                exceptionEnum.getCode(),
                exceptionEnum.getMsg(),
                null
        );
    }
    public static CommonResult fail(String exceptionEnum) {
        logger.info("执行失败："+exceptionEnum);
        return new CommonResult(
                500,
                exceptionEnum,
                null
        );
    }


}
